/*
** main.c for Mortal LIF7 in /home/minot_a/Fac
** 
** Made by yoann1 minot
** Login   <minot_a@epitech.net>
** 
** Started on  Mon Oct 18 16:44:52 2010 yoann1 minot
** Last update Thu Nov 11 00:59:08 2010 yoann1 minot
*/

#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <SDL.h>
#include "struc.h"

void		init_SDL(t_decor *s, t_perso *p)
{
  SDL_Init(SDL_INIT_VIDEO);
  s->ecran = SDL_SetVideoMode(WIDTH_WIN, HEIGHT_WIN, RESOLUTION, SDL_HWSURFACE | SDL_DOUBLEBUF);
  SDL_WM_SetCaption("Mortal LIF7",NULL);
  SDL_EnableKeyRepeat(10, 10);
  s->fond = SDL_LoadBMP("imgs/fond.bmp");
  p->sprite[0] = SDL_LoadBMP("imgs/sprite1.bmp");
  p->sprite[1] = SDL_LoadBMP("imgs/sprite2.bmp");
  s->life[0] = SDL_LoadBMP("imgs/life1.bmp");
  s->life[1] = SDL_LoadBMP("imgs/lifeg1.bmp");
  s->life[2] = SDL_LoadBMP("imgs/life2.bmp");
  s->life[3] = SDL_LoadBMP("imgs/lifeg2.bmp");
  s->pos_fond.x = 0;
  s->pos_fond.y = 0;
  p->pos_sprite[0].x = 0;
  p->pos_sprite[0].y = 0;
  p->pos_sprite[0].h = 200;
  p->pos_sprite[0].w = 200;
  p->pos_sprite[1].x = 600;
  p->pos_sprite[1].y = 0;
  p->pos_sprite[1].h = 200;
  p->pos_sprite[1].w = 200;
  s->pos_life1.y = 74;
  s->pos_life1.x = 74;
  s->pos_lifeg1.y = 74;
  s->pos_lifeg1.x = 74;
  s->pos_life2.y = 74;
  s->pos_life2.x = 548;
  s->pos_lifeg2.y = 74;
  s->pos_lifeg2.x = 548;
  p->nb_life1.x = 0;
  p->nb_life1.y = 0;
  p->nb_life1.w = 375;
  p->nb_life1.h = 60;
  p->nb_life2.x = 0;
  p->nb_life2.y = 0;
  p->nb_life2.w = (400 - 375);
  p->nb_life2.h = 60;
  p->pos_perso[0].x = 105;
  p->pos_perso[0].y = 550;
  p->pos_perso[1].x = 720;
  p->pos_perso[1].y = 550;
  SDL_BlitSurface(s->fond, NULL, s->ecran, &s->pos_fond);
  SDL_SetColorKey(p->sprite[0], SDL_SRCCOLORKEY, SDL_MapRGB(p->sprite[0]->format, 0, 50, 0));
  SDL_SetColorKey(p->sprite[1], SDL_SRCCOLORKEY, SDL_MapRGB(p->sprite[1]->format, 0, 50, 0));
  SDL_BlitSurface(p->sprite[0], &p->pos_sprite[0], s->ecran, &p->pos_perso[0]);
  SDL_BlitSurface(p->sprite[1], &p->pos_sprite[1], s->ecran, &p->pos_perso[1]);
  SDL_BlitSurface(s->life[0], NULL, s->ecran, &s->pos_life1);
  SDL_BlitSurface(s->life[1], &p->nb_life1, s->ecran, &s->pos_lifeg1);
  SDL_BlitSurface(s->life[3], NULL, s->ecran, &s->pos_life2);
  SDL_BlitSurface(s->life[2], &p->nb_life2, s->ecran, &s->pos_lifeg2);
  SDL_Flip(s->ecran);
}

void		aff_vie(t_decor *s, t_perso *p)
{
  SDL_BlitSurface(s->life[0], NULL, s->ecran, &s->pos_life1);
  SDL_BlitSurface(s->life[1], &p->nb_life1, s->ecran, &s->pos_lifeg1);
  SDL_BlitSurface(s->life[3], NULL, s->ecran, &s->pos_life2);
  SDL_BlitSurface(s->life[2], &p->nb_life2, s->ecran, &s->pos_lifeg2);
  SDL_UpdateRect(s->ecran, 74, 74, 874, 60);
}

void		aff_perso(t_decor *s, t_perso *p)
{
  SDL_BlitSurface(s->fond, NULL, s->ecran, &(s->pos_fond));
  SDL_SetColorKey(p->sprite[0], SDL_SRCCOLORKEY, SDL_MapRGB(p->sprite[0]->format, 0, 50, 0));
  SDL_SetColorKey(p->sprite[1], SDL_SRCCOLORKEY, SDL_MapRGB(p->sprite[1]->format, 0, 50, 0));
  SDL_BlitSurface(p->sprite[0], &p->pos_sprite[0], s->ecran, &p->pos_perso[0]);
  SDL_BlitSurface(p->sprite[1], &p->pos_sprite[1], s->ecran, &p->pos_perso[1]);
  SDL_UpdateRect(s->ecran, p->pos_perso[0].x - 20, p->pos_perso[0].y - 20, 240, 230);
  SDL_UpdateRect(s->ecran, p->pos_perso[1].x - 20, p->pos_perso[1].y - 20, 240, 230);
}

int		test_death(t_perso *p, int continuer)
{
  if (p->nb_life1.w <= 25)
    continuer = 2;
  if (p->nb_life2.w >= 375)
    {
      if (continuer == 3)
	continuer = 1;
      else if (continuer == 2)
	continuer = 4;
    }
  return (continuer);
}

void		jump(t_cpt_mv *cm, t_perso *p)
{
  int		j;

  for (j=0; j<=1; j++)
    {
      if (cm->saut[j] > 0)
	{
	  if (cm->saut[j] > 15)
	    {
	      p->pos_perso[j].y -= (cm->saut[j]*2 - 30);
	      cm->saut[j]--;
	    }
	  else
	    {
	      p->pos_perso[j].y += (30 - cm->saut[j]*2 + 2);
	      cm->saut[j]--;
	    }
	}
    }
}

int		test_collision(t_perso *p, int j)
{
  if (j == 0)
    {
      if (p->pos_sprite[0].x < 600 &&
	  (p->pos_perso[1].x >= p->pos_perso[0].x + 25 && 
	   p->pos_perso[1].x <= p->pos_perso[0].x + 75 &&
	   p->pos_perso[1].y <= p->pos_perso[0].y + 75 &&
	   p->pos_perso[1].y + 75 >= p->pos_perso[0].y))
	{
	  return (0);
	}
      if (p->pos_sprite[0].x >= 600 && 
	  (p->pos_perso[1].x + 75 >= p->pos_perso[0].x && 
	   p->pos_perso[1].x + 25 <= p->pos_perso[0].x &&
	   p->pos_perso[1].y <= p->pos_perso[0].y + 75 &&
	   p->pos_perso[1].y + 75 >= p->pos_perso[0].y))
	{
	  return (0);
	}
    }
  if (j == 1)
    {
      if (p->pos_sprite[1].x < 600 &&
	  (p->pos_perso[0].x >= p->pos_perso[1].x + 25 && 
	   p->pos_perso[0].x <= p->pos_perso[1].x + 75 &&
	   p->pos_perso[0].y <= p->pos_perso[1].y + 75 &&
	   p->pos_perso[0].y + 75 >= p->pos_perso[1].y))
	{
	  return (0);
	}
      if (p->pos_sprite[1].x >= 600 && 
	  (p->pos_perso[0].x + 75 >= p->pos_perso[1].x && 
	   p->pos_perso[0].x + 25 <= p->pos_perso[1].x &&
	   p->pos_perso[0].y <= p->pos_perso[1].y + 75 &&
	   p->pos_perso[0].y + 75 >= p->pos_perso[1].y))
	{
	  return (0);
	}
    }
  return (1);
}

void		test_hit(t_perso *p, int j, int range, int dmg)
{
  if (j == 0)
    {
      if (p->pos_sprite[0].x < 600 && 
	  (p->pos_perso[1].x >= p->pos_perso[0].x + 55 && 
	   p->pos_perso[1].x <= p->pos_perso[0].x + 115 + range &&
	   p->pos_perso[1].y <= p->pos_perso[0].y + 75 &&
	   p->pos_perso[1].y + 75 >= p->pos_perso[0].y))
	{
	  if (p->pos_sprite[1].x < 600 || p->pos_sprite[1].y != 600)
	    p->nb_life2.w += dmg;
	}
      if (p->pos_sprite[0].x >= 600 && 
	  (p->pos_perso[1].x + 115 + range >= p->pos_perso[0].x && 
	   p->pos_perso[1].x + 55 <= p->pos_perso[0].x &&
	   p->pos_perso[1].y <= p->pos_perso[0].y + 75 &&
	   p->pos_perso[1].y + 75 >= p->pos_perso[0].y))
	{
	  if (p->pos_sprite[1].x >= 600 || p->pos_sprite[1].y != 600)
	    p->nb_life2.w += dmg;
	}
    }
  if (j == 1)
    {
      if (p->pos_sprite[1].x < 600 && 
	  (p->pos_perso[0].x >= p->pos_perso[1].x + 55 && 
	   p->pos_perso[0].x <= p->pos_perso[1].x + 115 + range &&
	   p->pos_perso[0].y <= p->pos_perso[1].y + 75 &&
	   p->pos_perso[0].y + 75 >= p->pos_perso[1].y))
	{
	  if (p->pos_sprite[0].x < 600 || p->pos_sprite[0].y != 600)
	    p->nb_life1.w -= dmg;
	}
      if (p->pos_sprite[1].x >= 600 && 
	  (p->pos_perso[0].x + 115 + range >= p->pos_perso[1].x && 
	   p->pos_perso[0].x + 55 <= p->pos_perso[1].x &&
	   p->pos_perso[0].y <= p->pos_perso[1].y + 75 &&
	   p->pos_perso[0].y + 75 >= p->pos_perso[1].y))
	{
	  if (p->pos_sprite[0].x >= 600 || p->pos_sprite[0].y != 600)
	    p->nb_life1.w -= dmg;
	}
    }
}

void		coup_de_pied(t_cpt_mv *cm, t_perso *p)
{
  int		j;

  for (j=0; j<=1; j++)
    {
      if (cm->pied[j] > 1)
	{
	  p->pos_sprite[j].y = 400;
	  if (p->pos_sprite[j].x < 600)
	    {
	      if (cm->pied[j] == 7 || cm->pied[j] == 6)
		p->pos_sprite[j].x = 0;
	      if (cm->pied[j] == 5 || cm->pied[j] == 4)
		p->pos_sprite[j].x = 200;
	      if (cm->pied[j] == 3 || cm->pied[j] == 2)
		p->pos_sprite[j].x = 400;
	    }
	  if (p->pos_sprite[j].x >= 600)
	    {
	      if (cm->pied[j] == 7 || cm->pied[j] == 6)
		p->pos_sprite[j].x = 600;
	      if (cm->pied[j] == 5 || cm->pied[j] == 4)
		p->pos_sprite[j].x = 800;
	      if (cm->pied[j] == 3 || cm->pied[j] == 2)
		p->pos_sprite[j].x = 1000;
	    }
	  cm->pied[j]--;
	}
      if (cm->pied[j] == 1)
	{
	  p->pos_sprite[j].y = 0;
	  if (p->pos_sprite[j].x < 600)
	    p->pos_sprite[j].x = 0;
	  if (p->pos_sprite[j].x >= 600)
	    p->pos_sprite[j].x = 600;
	  cm->pied[j]--;
	}
      if (cm->pied[j] == 5)
	test_hit(p, j, 10, 15);
    }
}

void		coup_de_poing(t_cpt_mv *cm, t_perso *p)
{
  int		j;

  for (j=0; j<=1; j++)
    {
      if (cm->poing[j] > 0)
	{
	  p->pos_sprite[j].y = 200;
	  if (p->pos_sprite[j].x < 600)
	    {
	      if (cm->poing[j] == 4)
		p->pos_sprite[j].x = 0;
	      if (cm->poing[j] == 3)
		p->pos_sprite[j].x = 200;
	      if (cm->poing[j] == 2 || cm->poing[j] == 1)
		p->pos_sprite[j].x = 400;
	    }
	  if (p->pos_sprite[j].x >= 600)
	    {
	      if (cm->poing[j] == 4)
		p->pos_sprite[j].x = 600;
	      if (cm->poing[j] == 3)
		p->pos_sprite[j].x = 800;
	      if (cm->poing[j] == 2 || cm->poing[j] == 1)
		p->pos_sprite[j].x = 1000;
	    }
	  if (cm->poing[j] == 2)
	    test_hit(p, j, 0, 11);
	  cm->poing[j]--;
	}
    }
}

void		event_p1(t_perso *p, t_cpt_mv *cm, Uint8 *keystate)
{
  if (keystate[SDLK_LEFT])
    {
      if (p->pos_sprite[1].x < 600)
	p->pos_sprite[1].x = 600;
      else
	{
	  p->pos_sprite[1].x += 200;
	  if (p->pos_sprite[1].x >= 1200)
	    p->pos_sprite[1].x = 600;
	}
      if (p->pos_perso[1].x > 30 && test_collision(p, 1))
	p->pos_perso[1].x -= 15;
    }
  if (keystate[SDLK_RIGHT])
    {
      if (p->pos_sprite[1].x >= 600)
	p->pos_sprite[1].x = 0;
      else
	{
	  p->pos_sprite[1].x += 200;
	  if (p->pos_sprite[1].x >= 600)
	    p->pos_sprite[1].x = 0;
	}
      if (p->pos_perso[1].x < 790 && test_collision(p, 1))
	p->pos_perso[1].x += 15;
    }
  if (keystate[SDLK_SLASH])
    {
      if (cm->poing[1] == 0 && cm->pied[1] == 0)
	cm->poing[1] = 4;
    }
  if (keystate[SDLK_RCTRL])
    {
      if (cm->pied[1] == 0 && cm->poing[1] == 0)
	cm->pied[1] = 7;
    }
  if (keystate[SDLK_DOWN])
    {
      if (cm->pied[1] == 0 && cm->poing[1] == 0)
	{
	  p->pos_sprite[1].y = 600;
	  if (p->pos_sprite[1].x >= 600)
	    p->pos_sprite[1].x = 600;
	  if (p->pos_sprite[1].x < 600)
	    p->pos_sprite[1].x = 400;
	}
    }
  if (keystate[SDLK_UP])
    {
      p->pos_sprite[1].y = 0;
      if (cm->saut[1] == 0)
	cm->saut[1] = 30;
    }
}

void		event_p2(t_perso *p, t_cpt_mv *cm, Uint8 *keystate)
{
  if (keystate[SDLK_s])
    {
      if (p->pos_sprite[0].x < 600)
	p->pos_sprite[0].x = 600;
      else
	{
	  p->pos_sprite[0].x += 200;
	  if (p->pos_sprite[0].x >= 1200)
	    p->pos_sprite[0].x = 600;
	}
      if (p->pos_perso[0].x > 30 && test_collision(p, 0))
	p->pos_perso[0].x -= 15;
    }
  if (keystate[SDLK_f])
    {
      if (p->pos_sprite[0].x >= 600)
	p->pos_sprite[0].x = 0;
      else
	{
	  p->pos_sprite[0].x += 200;
	  if (p->pos_sprite[0].x >= 600)
	    p->pos_sprite[0].x = 0;
	}
      if (p->pos_perso[0].x < 790 && test_collision(p, 0))
	p->pos_perso[0].x += 15;
    }
  if (keystate[SDLK_q])
    {
      if (cm->poing[0] == 0 && cm->pied[0] == 0)
	cm->poing[0] = 4;
    }
  if (keystate[SDLK_a])
    {
      if (cm->pied[0] == 0 && cm->poing[0] == 0)
	cm->pied[0] = 7;
    }
  if (keystate[SDLK_d])
    {
      if (cm->pied[0] == 0 && cm->poing[0] == 0)
	{
	  p->pos_sprite[0].y = 600;
	  if (p->pos_sprite[0].x >= 600)
	    p->pos_sprite[0].x = 600;
	  if (p->pos_sprite[0].x < 600)
	    p->pos_sprite[0].x = 400;
	}
    }
  if (keystate[SDLK_e])
    {
      p->pos_sprite[0].y = 0;
      if (cm->saut[0] == 0)
	cm->saut[0] = 30;
    }
}

int		main_loop(t_decor *s, t_perso *p, t_cpt_mv *cm)
{
  int		continuer;
  int		j;
  Uint8		*keystate;
  SDL_Event	event;

  continuer = 3;
  cm->saut[0] = 0;
  cm->saut[1] = 0;
  cm->poing[0] = 0;
  cm->poing[1] = 0;
  cm->pied[0] = 0;
  cm->pied[1] = 0;
  keystate = SDL_GetKeyState(NULL);
  while (continuer == 3)
    {
      SDL_Delay(40);
      for (j=0; j<=1; j++)
	p->pos_sprite[j].y = 0;
      SDL_PollEvent(&event);
      if (event.type == SDL_QUIT)
	continuer = 0;
      event_p1(p, cm, keystate);
      event_p2(p, cm, keystate);
      coup_de_pied(cm, p);
      coup_de_poing(cm, p);
      jump(cm, p);
      continuer = test_death(p, continuer);
      aff_vie(s, p);
      aff_perso(s, p);
    }
  if (continuer == 1)
    s->fond = SDL_LoadBMP("imgs/finish1.bmp");
  if (continuer == 2)
    s->fond = SDL_LoadBMP("imgs/finish2.bmp");
  if (continuer == 4)
    s->fond = SDL_LoadBMP("imgs/finish3.bmp");
  if (continuer != 0)
    {
      SDL_BlitSurface(s->fond, NULL, s->ecran, &s->pos_fond);
      SDL_Flip(s->ecran);
      SDL_Delay(4000);
    }
  return (0);
}

int		main(void)
{
  t_decor	*s;
  t_perso	*p;
  t_cpt_mv	*cm;

  cm = malloc(sizeof(t_cpt_mv));
  p = malloc(sizeof(t_perso));
  s = malloc(sizeof(t_decor));
  init_SDL(s, p);
  main_loop(s, p, cm);
  SDL_Quit();
  return (0);
}
